package com.android.stockme.frontend;

import java.util.List;

import com.android.stockme.backend.BackendController;
import com.android.stockme.backend.IBackendController;
import com.android.stockme.backend.configuration.IConfigurationChangedListener;
import com.android.stockme.backend.configuration.IConfigurationManager;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * 
 * @author Avishay
 *
 * This class represents the Settings Tab of the application. It enables the user to configure application-level settings such as database
 * refresh rate and so.
 * 
 */
public class StockMeSettingsTab extends Activity {

	private StockMeSettingsTab instance = null;
	
	private IBackendController backend = null;

	private Button applyButton = null;
	private Button defaultsButton = null;
	
	private EditText newsTickFreqEdt = null;
	
	private EditText stockTickFreqEdt = null;

	private EditText newsHistoryLengthEdt = null;

	private Spinner financeWebsiteSpnr = null;
	private ArrayAdapter<String> financeWebsiteSpnrAdapter = null;
	
	private CheckBox notificationStateChbox = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.settings);

		this.instance = this;
		
		this.backend = BackendController.getBackendController();
		
		this.backend.getConfigurationManager().registerFinanceWebsiteChangedListener(new IConfigurationChangedListener() {
			public void notifyConfigurationChanged(Object arg) {

			}			
		});		
		this.backend.getConfigurationManager().registerHistoryLengthChangedListener(new IConfigurationChangedListener() {
			public void notifyConfigurationChanged(Object arg) {
				newsHistoryLengthEdt.setText(Integer.toString((Integer)arg));
			}			
		});
		this.backend.getConfigurationManager().registerNewsTickFrequencyChangedListener(new IConfigurationChangedListener() {
			public void notifyConfigurationChanged(Object arg) {
				newsTickFreqEdt.setText(Integer.toString((Integer)arg));
			}			
		});
		this.backend.getConfigurationManager().registerStockTickFrequencyChangedListener(new IConfigurationChangedListener() {
			public void notifyConfigurationChanged(Object arg) {
				stockTickFreqEdt.setText(Integer.toString((Integer)arg));
			}			
		});
		
		this.applyButton = (Button)this.findViewById(R.id.settings_apply_button);
		this.applyButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				validateAndSet();
			}
		});
		
		this.defaultsButton = (Button)this.findViewById(R.id.settings_defaults_button);
		this.defaultsButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				backend.getConfigurationManager().restoreDefault();
			}
		});

		this.newsTickFreqEdt = (EditText)this.findViewById(R.id.settings_news_tick_freq_edit);
		this.newsTickFreqEdt.setText(Integer.toString(this.backend.getConfigurationManager().getNewsTickFrequency()));

		this.stockTickFreqEdt = (EditText)this.findViewById(R.id.settings_stock_tick_freq_edit);		
		this.stockTickFreqEdt.setText(Integer.toString(this.backend.getConfigurationManager().getStockTickFrequency()));
		
		this.newsHistoryLengthEdt = (EditText)this.findViewById(R.id.settings_news_history_len_edit);		
		this.newsHistoryLengthEdt.setText(Integer.toString(this.backend.getConfigurationManager().getNewsHistoryLength()));
		
		this.financeWebsiteSpnr = (Spinner)this.findViewById(R.id.settings_finance_website_lst);
		this.financeWebsiteSpnrAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, this.backend.getConfigurationManager().getDataBaseFinanceWebSites());
		this.financeWebsiteSpnrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		this.financeWebsiteSpnr.setAdapter(this.financeWebsiteSpnrAdapter);
		this.financeWebsiteSpnr.setSelection(this.financeWebsiteSpnrAdapter.getPosition(this.backend.getConfigurationManager().getFinanceWebsite()));
		
		this.notificationStateChbox = (CheckBox)this.findViewById(R.id.settings_notification_state_chbox);
		this.notificationStateChbox.setChecked(this.backend.getConfigurationManager().getNotificationState());
	}
	
	private void validateAndSet() {
		IConfigurationManager configuration = this.backend.getConfigurationManager();
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this.instance);
		dialogBuilder.setCancelable(false);
		dialogBuilder.setTitle("Error");
		dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		AlertDialog dialog = dialogBuilder.create();
				
		int newsTickFreq = Integer.parseInt(newsTickFreqEdt.getText().toString());
		int stockTickFreq = Integer.parseInt(stockTickFreqEdt.getText().toString());
		int newsHistoryLength = Integer.parseInt(newsHistoryLengthEdt.getText().toString());
		
		if (newsTickFreq < 30 || newsTickFreq < 0) {
			dialog.setMessage("News tick frequency must be larger than 30 seconds");
			dialog.show();
			return;
		}
		if (stockTickFreq < 30 || stockTickFreq < 0) {
			dialog.setMessage("Stock tick frequency must be larger than 30 seconds");
			dialog.show();
			return;
		}
		if (newsHistoryLength > 1000 || newsHistoryLength < 0) {
			dialog.setMessage("News history length must be smaller than 1000 news updates");
			dialog.show();
			return;
		}

		configuration.setNewsTickFrequency(newsTickFreq);
		configuration.setStockTickFrequency(stockTickFreq);
		configuration.setNewsHistoryLength(newsHistoryLength);
		configuration.setFinanceWebsite((String)this.financeWebsiteSpnr.getSelectedItem());
		configuration.setNotificationState(this.notificationStateChbox.isChecked());
	}
}
